package service

import (
	"fmt"
	"runtime"

	"gitee.com/liumou_site/logger"
)

// PrintServiceManagementCommands 打印当前系统的服务管理命令
func PrintServiceManagementCommands(serviceName string) {
	if serviceName == "" {
		serviceName = "PasswordReset"
	}
	logger.Info("=== 当前系统的服务状态管理命令 ===")

	switch runtime.GOOS {
	case "windows":
		printWindowsServiceCommands(serviceName)
	case "linux":
		printLinuxServiceCommands(serviceName)
	case "darwin":
		printMacOSServiceCommands(serviceName)
	default:
		logger.Warn("不支持的操作系统: %s", runtime.GOOS)
	}
}

// printWindowsServiceCommands 打印Windows系统的服务状态管理命令
func printWindowsServiceCommands(serviceName string) {
	logger.Info("【Windows 服务状态管理】")
	logger.Info("")

	logger.Info("1. 查询Windows服务状态：")
	logger.Info("   sc query %s", serviceName)
	logger.Info("")

	logger.Info("2. 查询Windows任务计划状态：")
	logger.Info("   schtasks /query /tn %s", serviceName)
	logger.Info("")

	logger.Info("3. 启动Windows服务：")
	logger.Info("   sc start %s", serviceName)
	logger.Info("")

	logger.Info("4. 停止Windows服务：")
	logger.Info("   sc stop %s", serviceName)
	logger.Info("")

	logger.Info("5. 服务文件路径：")
	logger.Info("   %s", getDefaultServicePath())
}

// printLinuxServiceCommands 打印Linux系统的服务状态管理命令
func printLinuxServiceCommands(serviceName string) {
	logger.Info("【Linux 服务状态管理】")
	logger.Info("")

	logger.Info("1. 查看服务状态：")
	logger.Info("   sudo systemctl status %s", serviceName)
	logger.Info("")

	logger.Info("2. 查看服务日志：")
	logger.Info("   sudo journalctl -u %s -f", serviceName)
	logger.Info("")

	logger.Info("3. 启动服务：")
	logger.Info("   sudo systemctl start %s", serviceName)
	logger.Info("")

	logger.Info("4. 停止服务：")
	logger.Info("   sudo systemctl stop %s", serviceName)
	logger.Info("")

	logger.Info("5. 重启服务：")
	logger.Info("   sudo systemctl restart %s", serviceName)
	logger.Info("")

	logger.Info("6. 服务文件路径：")
	logger.Info("   %s", getDefaultServicePath())
}

// printMacOSServiceCommands 打印macOS系统的服务状态管理命令
func printMacOSServiceCommands(serviceName string) {
	logger.Info("【macOS 服务状态管理】")
	logger.Info("")

	logger.Info("1. 查看服务状态：")
	logger.Info("   sudo launchctl list | grep %s", serviceName)
	logger.Info("")

	logger.Info("2. 查看服务日志：")
	logger.Info("   sudo log show --predicate 'process == \"%s\"' --info", serviceName)
	logger.Info("")

	logger.Info("3. 启动服务：")
	logger.Info("   sudo launchctl start %s", serviceName)
	logger.Info("")

	logger.Info("4. 停止服务：")
	logger.Info("   sudo launchctl stop %s", serviceName)
	logger.Info("")

	logger.Info("5. 重启服务：")
	logger.Info("   sudo launchctl stop %s", serviceName)
	logger.Info("   sudo launchctl start %s", serviceName)
	logger.Info("")

	logger.Info("6. 服务文件路径：")
	logger.Info("   %s", getDefaultServicePath())
}

// GetServiceInstallCommands 获取服务安装命令（已移除，仅保留状态管理）
func GetServiceInstallCommands(interval int) []string {
	return []string{"使用程序自动创建服务功能，无需手动安装"}
}

// GetServiceStatusCommands 获取服务状态查询命令
func GetServiceStatusCommands(serviceName string) []string {
	if serviceName == "" {
		serviceName = "PasswordReset"
	}

	switch runtime.GOOS {
	case "windows":
		return []string{
			fmt.Sprintf("sc query %s", serviceName),
			fmt.Sprintf("schtasks /query /tn %s", serviceName),
		}
	case "linux":
		return []string{
			fmt.Sprintf("sudo systemctl status %s", serviceName),
			fmt.Sprintf("sudo journalctl -u %s -f", serviceName),
		}
	case "darwin":
		return []string{
			fmt.Sprintf("sudo launchctl list | grep %s", serviceName),
			fmt.Sprintf("sudo log show --predicate 'process == \"%s\"' --info", serviceName),
		}
	default:
		return []string{"不支持的操作系统"}
	}
}

// GetServiceUninstallCommands 获取服务卸载命令（已移除，仅保留状态管理）
func GetServiceUninstallCommands() []string {
	return []string{"使用程序自动删除服务功能，无需手动卸载"}
}
